#include "stdafx.h"
#include "Timer.h"

Timer::Timer(void) :
	m_n_FPS(0),
	m_f_StartTime(0.0f),
	m_f_DeltaTime(0.0f),
	m_f_GlobalTime(0.0f),
	m_n_TickFrequency()
{
}

Timer::~Timer(void)
{
}

void Timer::Reset()
{
    LARGE_INTEGER ticks;

    // get processor frequency
    QueryPerformanceFrequency(&m_n_TickFrequency);

    // get current tick count
    QueryPerformanceCounter(&ticks);

    // init time at start
    m_f_StartTime = (float)ticks.QuadPart / (float)m_n_TickFrequency.QuadPart;

    // reset frame count
    m_n_FPS = 0;
	m_f_GlobalTime = 0.0f;
}

void Timer::Update()
{
    // locals
    static float   timePrev = GetCurrentTime();
    static float   timeLastFrame = GetCurrentTime();
    static int     frames = 0;
    float          timeNow = GetCurrentTime();

    // increment frame count
    frames++;

    // log how many frames since ONE second
    if ( timeNow - timePrev > 1.0f )
    {
        m_n_FPS = frames / (int)(timeNow - timePrev);
        frames = 0;
        timePrev = timeNow;
    }

    // calculate delta time
    m_f_DeltaTime = ( timeNow - timeLastFrame );
	m_f_GlobalTime += m_f_DeltaTime;
    timeLastFrame = timeNow;
}

float Timer::GetCurrentTime()
{
    LARGE_INTEGER   ticks;
    float           time;

    // get current tick count
    QueryPerformanceCounter( &ticks );

    // get current time ( since computer turned on )
    time = ( float )ticks.QuadPart / ( float )m_n_TickFrequency.QuadPart;

    // calculate time since reset
    return (time - m_f_StartTime);
}


